package skewtune.utils;

/**
 * from http://www.cs.princeton.edu/introcs/22library/Gaussian.java.html
 *
 */
public final class Gaussian {
	public static final double DEFAULT_ERROR = .00000001;

    // return phi(x) = standard Gaussian pdf
    public static double phi(double x) {
        return Math.exp(-x*x / 2) / Math.sqrt(2 * Math.PI);
    }

    // return phi(x, mu, signma) = Gaussian pdf with mean mu and stddev sigma
    public static double phi(double x, double mu, double sigma) {
        return phi((x - mu) / sigma) / sigma;
    }

    // return Phi(z) = standard Gaussian cdf using Taylor approximation
    public static double Phi(double z) {
        if (z < -8.0) return 0.0;
        if (z >  8.0) return 1.0;
        double sum = 0.0, term = z;
        for (int i = 3; sum + term != sum; i += 2) {
            sum  = sum + term;
            term = term * z * z / i;
        }
        return 0.5 + sum * phi(z);
    }

    // return Phi(z, mu, sigma) = Gaussian cdf with mean mu and stddev sigma
    public static double Phi(double z, double mu, double sigma) {
        return Phi((z - mu) / sigma);
    } 

    // Compute z such that Phi(z) = y via bisection search
    public static double PhiInverse(double y) {
        return PhiInverse(y, DEFAULT_ERROR, -8, 8);
    } 
    
    public static double PhiInverse(double y,double err) {
        return PhiInverse(y, err, -8, 8);
    } 

    public static double PhyInverse(double y,double mu,double sigma) {
    	return PhyInverse(y, DEFAULT_ERROR, -8, 8) * sigma + mu;
    }

    public static double PhyInverse(double y,double mu,double sigma,double err) {
    	return PhyInverse(y,err,-8,8) * sigma + mu;
    }

    // bisection search
    private static double PhiInverse(double y, double delta, double lo, double hi) {
        double mid = lo + (hi - lo) / 2;
        if (hi - lo < delta) return mid;
        if (Phi(mid) > y) return PhiInverse(y, delta, lo, mid);
        else              return PhiInverse(y, delta, mid, hi);
    }
}
